package statistics;

import java.util.Random;


public class PoissonDistribution extends Distribution{

    protected double lambda;
    
    public PoissonDistribution( double lambda, Random random ){
        this.lambda = lambda;
        this.random = random;
 
    }
            

    public double expectation() {
       return lambda;
    }


    public double variance() {
        return lambda;
    }


    public double nextRandom() {
        double U = random.nextDouble(); ;
        int counter=0;
        double p=Math.exp(-lambda);
        double sum=p;
        
        while(sum<U){
          p = p*lambda /(counter+1);
          sum = sum + p;
          counter++;
        }
        return counter;
                
    }
        
}
    
    
